﻿//-----------------------------------------------------------------------
// <copyright file="Logger.cs" company="Microsoft Corporation">
// Copyright (c) Microsoft Corporation.
// All rights reserved.
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY 
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
// This code released under the terms of the 
// Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.)
// </copyright>
// <author>Medha</author>
// <date>16-May-2011</date>
//-----------------------------------------------------------------------

namespace SmartTag.Common.Utility
{
    using System;
    using Microsoft.Practices.ServiceLocation;
    using Microsoft.Practices.SharePoint.Common.Logging;
    using Microsoft.Practices.SharePoint.Common.ServiceLocation;
    using Microsoft.SharePoint.Administration;

    /// <summary>
    /// EventIds for identifying the error event
    /// </summary>
    public enum TraceLogEventId
    {
        /// <summary>
        /// Error in reading the file content
        /// </summary>
        ErrorInFileReading = 100,

        /// <summary>
        /// Error in LoadConfiguration
        /// </summary>
        LoadConfiguration = 101,

        /// <summary>
        /// Error in SaveConfiguration 
        /// </summary>
        SaveConfiguration = 102,

        /// <summary>
        /// Error in ClearConfiguration 
        /// </summary>
        ClearConfiguration = 103,

        /// <summary>
        /// Error in ItemAdded 
        /// </summary>
        ItemAdded = 104,

        /// <summary>
        /// Error in GetManagedMetadata 
        /// </summary>
        GetManagedMetadata = 105,

        /// <summary>
        /// Error in GetWords 
        /// </summary>
        GetWords = 106,

        /// <summary>
        /// Error in RefreshUI 
        /// </summary>
        RefreshUI = 107,

        /// <summary>
        /// Error in FillManagedMetadataColumnsDdl 
        /// </summary>
        FillManagedMetadataColumnsDdl = 108,

        /// <summary>
        /// Error in FillCheckBoxList 
        /// </summary>
        FillCheckBoxList = 109,

        /// <summary>
        /// Error in DeleteConfiguration 
        /// </summary>
        DeleteConfiguration = 110,

        /// <summary>
        /// Error in TaggingMethod 
        /// </summary>
        TaggingMethod = 111,

        /// <summary>
        /// Error in LoadTerms 
        /// </summary>
        LoadTerms = 112,

        /// <summary>
        /// Error in TryGetTermsRecursively 
        /// </summary>
        TryGetTermsRecursively = 113,

        /// <summary>
        /// Error in TryGetDescendantTerms 
        /// </summary>
        TryGetDescendantTerms = 114,

        /// <summary>
        /// Error in GetTermStore 
        /// </summary>
        GetTermStore = 115,

        /// <summary>
        /// Error in ReadStringValueFromLocalMachine 
        /// </summary>
        ReadStringValueFromLocalMachine = 116,

        /// <summary>
        /// Error in LoadFilter 
        /// </summary>
        LoadFilter = 117,

        /// <summary>
        /// Error in LoadConfiguration 
        /// </summary>
        GetPHIdFromDocType = 118,

        /// <summary>
        /// Error in LoadFilterComponent 
        /// </summary>
        LoadFilterComponent = 119,

        /// <summary>
        /// Error in InitializeFromStream 
        /// </summary>
        InitializeFromStream = 120,

        /// <summary>
        /// Error in ReadFilterStream 
        /// </summary>
        ReadFilterStream = 121,

        /// <summary>
        /// Error in ReadWords 
        /// </summary>
        ReadWords = 122,

        /// <summary>
        /// Error in GetClassFactoryFromDll 
        /// </summary>
        GetClassFactoryFromDll = 123,
    }

    /// <summary>
    /// Helper for logging in Sharepoint ULS logs
    /// </summary>
    public class Logger
    {
        /// <summary>
        /// Logs the error.
        /// </summary>
        /// <param name="ex">The Exception ex.</param>
        /// <param name="message">The additional message.</param>
        /// <param name="eventId">The event id.</param>
        public static void LogError(Exception ex, string message, int eventId)
        {
            IServiceLocator serviceLocator = SharePointServiceLocator.GetCurrent();
            ILogger logger = serviceLocator.GetInstance<ILogger>();
            string areaCategory = string.Format("{0}/{1}", "SmartTag", "Error");
            logger.TraceToDeveloper(ex, message, eventId, TraceSeverity.Unexpected, areaCategory);
        }       
    }
}
